<?php
session_start();

function nombre($n)
{
    return str_pad(mt_rand(0,pow(10,$n)-1),$n,'0',STR_PAD_LEFT);
}

function image($mot)
{    
    $size = 32;
    $marge = 5;
    
    $box = imagettfbbox($size, 0, 'fonts/smartie.ttf', $mot);
    $largeur = $box[2] - $box[0];
    $hauteur = $box[1] - $box[7];
    
    
    $img = imagecreate($largeur+$marge*2, $hauteur+$marge*2);
    $blanc = imagecolorallocate($img, 255, 255, 255); 
    $noir = imagecolorallocate($img, 0, 0, 0);
    
    imagettftext($img, $size, 0,$marge,$hauteur+$marge, $noir, 'fonts/smartie.ttf', $mot);   

    imagepng($img);
    imagedestroy($img);
}

function generateWord($wordSize)
{
    $result = "";
    for($i = 0; $i < $wordSize; $i++)
    {
        $result .= randomSign();
    }
    
    return $result;
}

function randomSign()
{
    $str = "ABCEFGHIJKLMNPQRSTUVWXYZ123456789";
    
   return $str[rand(0, strlen($str) - 1)];
}


function captcha()
{
    $mot = generateWord(6);
    $_SESSION['captcha'] = $mot;
    image($mot);
}

header("Content-type: image/png");
captcha();
?>